<!DOCTYPE html>
<html>
	<head>
		<title>Photo album online | Login Page</title>
		<link rel="stylesheet" type="text/css" href="homePageStyles.css" />
	</head>
	<body>
		<nav>
			<ul>
				<li><a href="registerPage.php">Register</a></li>
				<li><a href="loginPage.php">Login</a></li>
				<li><a href="#">Home</a></li>
			</ul>
		</nav>
		<div>User Created Successfully! Please login with you username and password:</div>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<label for="username">Username: </label>
			<input type="text" name="username" id="username"/><br />
			<label for="password">Password: </label>
			<input type="password" name="password" id="password"/><br />
			<input type="submit" name="submit" id="submit" value="Log In" />
		</form>
			
		<?php 
			function checkUser($username, $password) {
				global $dbConnection;
				$query = "Select * 
						from users 
						where username = '$username' 
						and password = '$password';";
				$statement = $dbConnection->execute($query);
				$count = $statement->num_rows();
				
				if($count == 1) {
					session_start();
					$_SESSION['username'] = $username;
					$_SESSION['password'] = $password;
					header("location:loginSuccess.php");
				}
				else {
					echo "<p>Username And Password Does Not Match!</p>";
				}
			}
		
			if(isset($_POST['submit'])) {
				$username = $_POST['username'];
				$password = sha1($_POST['password']);
				
				require_once 'db-connection.php';
				
				checkUser($username, $password);
			}
		?>
	</body>
</html>
